package com.playlist.listainteligente.condicao;

import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 *
 * @author rudieri
 */
public class JSelecionaCondicao extends javax.swing.JPanel implements ActionListener {

    private Condicao condicao;

    /**
     * Creates new form JSelecionaCondicao
     */
    public JSelecionaCondicao() {
        initComponents();
    }

    public void setCondicao(Condicao condicao) {
        this.condicao = condicao;
        jTextField_Condicao1.setText(condicao == null ? "" : condicao.toString());
    }

    public Condicao getCondicao() {
        return condicao;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField_Condicao1 = new javax.swing.JTextField();
        jButton_Condicao1 = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());
        add(jTextField_Condicao1, java.awt.BorderLayout.CENTER);

        jButton_Condicao1.setText("?");
        jButton_Condicao1.addActionListener(this);
        add(jButton_Condicao1, java.awt.BorderLayout.EAST);
    }

    // Code for dispatching events from components to event handlers.

    public void actionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == jButton_Condicao1) {
            JSelecionaCondicao.this.jButton_Condicao1ActionPerformed(evt);
        }
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_Condicao1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_Condicao1ActionPerformed
        final JDialog jDialog = new JDialog((JFrame) null, true);
        jDialog.getRootPane().setLayout(new BoxLayout(jDialog.getRootPane(), BoxLayout.Y_AXIS));
        final JCondicao jCondicao = new JCondicao();
        jCondicao.setCondicao(condicao);
        jDialog.getRootPane().add(jCondicao);
        JPanel jPanelBts = new JPanel();
        jPanelBts.setLayout(new BoxLayout(jPanelBts, BoxLayout.X_AXIS));
        final JButton jButton_Ok = new JButton("OK");
        jPanelBts.add(jButton_Ok);
        final JButton jButton_Cancel = new JButton("Cancelar");
        jPanelBts.add(jButton_Cancel);
        jDialog.getRootPane().add(jPanelBts);
        jDialog.pack();
        Window windowAncestor = SwingUtilities.getWindowAncestor(this);

        Rectangle bounds = windowAncestor.getBounds();
        jDialog.setLocation(bounds.x + bounds.width / 2 - jDialog.getWidth() / 2,
                bounds.y + bounds.height / 2 - jDialog.getHeight() / 2);
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == jButton_Ok) {
                    setCondicao(jCondicao.getCondicao());
                }
                jDialog.dispose();
            }
        };
        jButton_Ok.addActionListener(actionListener);
        jButton_Cancel.addActionListener(actionListener);
        jDialog.setVisible(true);
    }//GEN-LAST:event_jButton_Condicao1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Condicao1;
    private javax.swing.JTextField jTextField_Condicao1;
    // End of variables declaration//GEN-END:variables
}
